package cn.com.ninng;

import cn.com.ninng.common.RandomNode;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author liulekang
 * @createdate 2021/7/21
 */
public class Problem138 {




    public static RandomNode copyRandomList(RandomNode head) {

        RandomNode tmp = head;
        HashMap<RandomNode,RandomNode> randomNodeMaps = new HashMap<>();
        while (tmp != null) {
            RandomNode newNode = new RandomNode(tmp.val);
            randomNodeMaps.put(tmp,newNode);
            tmp = tmp.next;
        }

        tmp = head;
        while (tmp != null){
            RandomNode randomNode = randomNodeMaps.get(tmp);
            randomNode.next = randomNodeMaps.get(tmp.next);
            randomNode.random = randomNodeMaps.get(tmp.random);
            tmp = tmp.next;
        }
        return randomNodeMaps.get(head);
    }


}
